﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;

namespace LabelBar.View
{
    public class MainViewVM : ViewModelBase
    {
        private ObservableCollection<DocumentWindow> infos;
         public  ObservableCollection<DocumentWindow> Infos 
        {
            set { infos = value; RaisePropertyChanged("Infos");  }
            get {return infos; }
        }
         public Autodesk.AutoCAD.ApplicationServices.Document doc { set; get; }
        public MainViewVM()
        {
            infos = new ObservableCollection<DocumentWindow>();
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentWindowCollection.CollectionChanged += DocumentWindowCollection_CollectionChanged;
             doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
           // infos.Add(new Info() {  });
             infos = getwindows();

           
        }


        public ObservableCollection<DocumentWindow>  getwindows()
        {
            ObservableCollection<DocumentWindow> windows = new ObservableCollection<DocumentWindow>();

            foreach (DocumentWindow item in Application.DocumentWindowCollection)
            {
                windows.Add(item);
            }
            return windows;
        }

        private void DocumentWindowCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            infos = getwindows();
        }

        private RelayCommand<Document> choosedwgview;
        public RelayCommand<Document> ChooseDwgView
        {
            get
            {
                if (choosedwgview == null)
                {
                    choosedwgview = new RelayCommand<Document>(it =>
                     {
                         if (it==null)
                         {
                             return;
                         }
                         Application.DocumentManager.MdiActiveDocument = it;
                     }
                    );
                }

                return choosedwgview;
            }
        }
    }
}
